<template>
  <div class="chart" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts';
require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/util/util'

export default {
  props: {
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '100px'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.initChart()
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  watch: {
    taskVal: {
      handler: function() {
        this.initChart()
      },
      deep: true
    }
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.chart.setOption({
        grid: {
          top: 10,
          bottom: 30,
          left: 50,
          right: 20
        },
        legend: {
          data: ['省内', '省外'],
          textStyle: {
            color: '#fff',
          },
        },
        xAxis: [
          {
            type: 'category',
            boundaryGap: false,
            data: ['11-1', '11-2', '11-3', '11-4', '11-5', '11-6', '11-7'],
            splitLine: {
              lineStyle: {
                color: ['#666666']
              }
            },
            axisLine: {
              lineStyle: {
                color: '#ffffff',
              }
            }
          }
        ],
        yAxis: [
          {
            type: 'value',
            interval: 2000,
            splitLine: {
              lineStyle: {
                color: ['#666666']
              }
            },
            axisLine: {
              lineStyle: {
                color: '#ffffff',
              }
            }
          }
        ],
        series: [
          {
            name: '省内',
            type: 'line',
            stack: 'Total',
            areaStyle: {},
            emphasis: {
              focus: 'series'
            },
            data: [200, 1320, 1010, 1340, 900, 2300, 2100]
          },
          {
            name: '省外',
            type: 'line',
            stack: 'Total',
            areaStyle: {},
            emphasis: {
              focus: 'series'
            },
            data: [220, 4182, 5191, 3234, 1290, 330, 4310]
          }
        ]
      })
    }
  }
}
</script>
